package com.czh.controller;

import com.czh.constant.CommonConstant;
import com.czh.utils.RabbitMqUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author chenzhihua
 * @Date 2024/7/26
 */
@RestController
@RequestMapping(value = "rabbitmq")
public class SendMessageController {

    @Resource
    private RabbitMqUtils rabbitMqUtils;

    /**
     * 正常发送消息，正常消费
     *
     * @return
     */
    @GetMapping(value = "send/message")
    public String sendMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitMqUtils.sendByRoutingKey(CommonConstant.EXCHANGE_NAME, CommonConstant.TOPIC_TEST1_ROUTINGKEY_TEST, map);
        return "ok";
    }


    /**
     * 正常发送，异常消费
     *
     * @return
     */
    @GetMapping(value = "send/message2")
    public String sendMessage2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message2, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitMqUtils.sendByRoutingKey(CommonConstant.EXCHANGE_NAME, CommonConstant.TOPIC_TEST2_ROUTINGKEY_TEST, map);
        return "ok";
    }

    /**
     * 发送延迟消息
     *
     * @return
     */
    @GetMapping(value = "send/lazy/message2")
    public String sendlazyMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message2, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        // 设置延迟时间
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration("10000");
        Message message = new Message(map.toString().getBytes(), messageProperties);
        // 原始交换机发送消息到延迟队列(延迟队列的route_key)
        rabbitMqUtils.sendByRoutingKey(CommonConstant.EXCHANGE_NAME, CommonConstant.LAZY_ROUTE_KEY, message);
        return "ok";
    }

}
